package com.ruoyi.warehouse.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.course.domain.LwCourse;
import com.ruoyi.course.mapper.LwCourseMapper;
import com.ruoyi.materials.domain.LwMaterialInfo;
import com.ruoyi.sfproduct.domain.LwSfproduct;
import com.ruoyi.sfproduct.mapper.LwSfproductMapper;
import com.ruoyi.warehouse.domain.LwWarehousinginfo;
import com.ruoyi.warehouse.mapper.LwSfpWarehouseMapper;
import com.ruoyi.warehouse.mapper.LwWarehousinginfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.warehouse.domain.LwSfpWarehouse;
import com.ruoyi.warehouse.service.ILwSfpWarehouseService;

/**
 * 半成品仓库管理Service业务层处理
 * 
 * @author sll
 * @date 2023-09-07
 */
@Service
public class LwSfpWarehouseServiceImpl implements ILwSfpWarehouseService 
{
    @Autowired
    private LwSfpWarehouseMapper lwSfpWarehouseMapper;

    @Autowired
    private LwCourseMapper lwCourseMapper;

    @Autowired
    private LwWarehousinginfoMapper LwWarehousinginfoMapper;

    @Autowired
    private LwSfproductMapper lwSfproductMapper;

    /**
     * 查询半成品仓库管理
     * 
     * @param id 半成品仓库管理主键
     * @return 半成品仓库管理
     */
    @Override
    public LwSfpWarehouse selectLwSfpWarehouseById(Long id)
    {
        return lwSfpWarehouseMapper.selectLwSfpWarehouseById(id);
    }

    /**
     * 查询半成品仓库管理列表
     * 
     * @param lwSfpWarehouse 半成品仓库管理
     * @return 半成品仓库管理
     */
    @Override
    public List<LwSfpWarehouse> selectLwSfpWarehouseList(LwSfpWarehouse lwSfpWarehouse)
    {
        return lwSfpWarehouseMapper.selectLwSfpWarehouseList(lwSfpWarehouse);
    }

    /**
     * 新增半成品仓库管理
     * 
     * @param lwSfpWarehouse 半成品仓库管理
     * @return 结果
     */
    @Override
    public int insertLwSfpWarehouse(LwSfpWarehouse lwSfpWarehouse)
    {
        if (lwSfpWarehouse.getQuantity() != 0){
            // 添加半成品入库信息记录
            LwWarehousinginfo warehousinginfo = new LwWarehousinginfo();
            warehousinginfo.setCourseId(lwSfpWarehouse.getCourseId());
            warehousinginfo.setSfproductName(lwSfpWarehouse.getSfproductName());
            warehousinginfo.setModel(lwSfproductMapper.selectLwSfproductBySfproductId(lwSfpWarehouse.getSfproductId()).getModel());
            warehousinginfo.setParameter(lwSfproductMapper.selectLwSfproductBySfproductId(lwSfpWarehouse.getSfproductId()).getParameter());
            warehousinginfo.setTime(new Date());
            warehousinginfo.setEmployeeId(SecurityUtils.getUserId());
            warehousinginfo.setFlow(0L);
            warehousinginfo.setCompleted(lwSfpWarehouse.getQuantity());
            warehousinginfo.setRemark(lwSfpWarehouse.getRemark());
            lwSfpWarehouseMapper.insertWarehousinginfo(warehousinginfo);
        }

        LwSfpWarehouse isEmpty = lwSfpWarehouseMapper.selectLwSfpWarehouseBySfproductId(lwSfpWarehouse.getSfproductId());
        if (isEmpty == null){
            lwSfpWarehouse.setRemark(null);

            LwSfproduct lwSfproduct = lwSfproductMapper.selectLwSfproductBySfproductId(lwSfpWarehouse.getSfproductId());
            lwSfpWarehouse.setSfproductName(lwSfproduct.getSfproductName());
            lwSfpWarehouse.setModel(lwSfproduct.getModel());
            lwSfpWarehouse.setParameter(lwSfproduct.getParameter());

            return lwSfpWarehouseMapper.insertLwSfpWarehouse(lwSfpWarehouse);
        }else if (isEmpty.getSfproductId().equals(lwSfpWarehouse.getSfproductId())&& !Objects.equals(isEmpty.getIfSelfProduced(), lwSfpWarehouse.getIfSelfProduced())) {
            LwSfproduct lwSfproduct = lwSfproductMapper.selectLwSfproductBySfproductId(lwSfpWarehouse.getSfproductId());
            lwSfpWarehouse.setSfproductName(lwSfproduct.getSfproductName());
            lwSfpWarehouse.setModel(lwSfproduct.getModel());
            lwSfpWarehouse.setParameter(lwSfproduct.getParameter());
            return lwSfpWarehouseMapper.insertLwSfpWarehouse(lwSfpWarehouse);
        }else {

            Long quantity = isEmpty.getQuantity() + lwSfpWarehouse.getQuantity();
            lwSfpWarehouse.setQuantity(quantity);
            lwSfpWarehouse.setId(isEmpty.getId());
            lwSfpWarehouse.setRemark(isEmpty.getRemark());
            return lwSfpWarehouseMapper.updateLwSfpWarehouse(lwSfpWarehouse);
        }
    }

    /**
     * 修改半成品仓库管理
     * 
     * @param lwSfpWarehouse 半成品仓库管理
     * @return 结果
     */
    @Override
    public int updateLwSfpWarehouse(LwSfpWarehouse lwSfpWarehouse)
    {
        return lwSfpWarehouseMapper.updateLwSfpWarehouse(lwSfpWarehouse);
    }

    /**
     * 批量删除半成品仓库管理
     * 
     * @param ids 需要删除的半成品仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteLwSfpWarehouseByIds(Long[] ids)
    {
        return lwSfpWarehouseMapper.deleteLwSfpWarehouseByIds(ids);
    }

    /**
     * 删除半成品仓库管理信息
     * 
     * @param id 半成品仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteLwSfpWarehouseById(Long id)
    {
        return lwSfpWarehouseMapper.deleteLwSfpWarehouseById(id);
    }

    @Override
    public int outSfpWarehouse(LwSfpWarehouse lwSfpWarehouse) {
        LwSfpWarehouse sfpWarehouse = lwSfpWarehouseMapper.selectLwSfpWarehouseBySfproductId(lwSfpWarehouse.getSfproductId());
        // 出库数量大于库存数
        if (sfpWarehouse.getQuantity() < lwSfpWarehouse.getQuantity()){
            return -1;
        }else {
            // 设置库存数量
            long quantity = sfpWarehouse.getQuantity() - lwSfpWarehouse.getQuantity();
            sfpWarehouse.setQuantity(quantity);

            // 设置记录行备注
            LwCourse lwCourse = lwCourseMapper.selectLwCourseById(lwSfpWarehouse.getCourseId());
            lwCourse.setRemark(lwSfpWarehouse.getRemark());
            lwCourseMapper.updateLwCourse(lwCourse);

            // 保存半成品出库记录
            LwWarehousinginfo warehousinginfo = new LwWarehousinginfo();
            warehousinginfo.setCourseId(lwSfpWarehouse.getCourseId());
            warehousinginfo.setSfproductName(lwSfpWarehouse.getSfproductName());
            warehousinginfo.setModel(lwSfproductMapper.selectLwSfproductBySfproductId(lwSfpWarehouse.getSfproductId()).getModel());
            warehousinginfo.setTime(new Date());
            warehousinginfo.setEmployeeId(SecurityUtils.getUserId());
            warehousinginfo.setFlow(2L);
            warehousinginfo.setCompleted(lwSfpWarehouse.getQuantity());
            warehousinginfo.setRemark(lwSfpWarehouse.getRemark());
            lwSfpWarehouseMapper.insertWarehousinginfo(warehousinginfo);

            return lwSfpWarehouseMapper.updateLwSfpWarehouse(sfpWarehouse);
        }



    }

    @Override
    public int updateLwSfpWarehouseQuantity(LwSfpWarehouse lwSfpWarehouse) {
        if (lwSfpWarehouse.getVariable() < 0 || lwSfpWarehouse.getRemove() < 0){
            return -1;
        }
        if (lwSfpWarehouse.getRemove() > lwSfpWarehouse.getQuantity()){
            return -2;
        }
        // 新建仓库记录对象
        LwWarehousinginfo warehousingInfo = new LwWarehousinginfo();
        warehousingInfo.setSfproductName(lwSfpWarehouse.getSfproductName());
        warehousingInfo.setModel(lwSfpWarehouse.getModel());
        warehousingInfo.setParameter(lwSfpWarehouse.getParameter());
        warehousingInfo.setTime(new Date());
        warehousingInfo.setEmployeeId(SecurityUtils.getUserId());
        warehousingInfo.setRemark(lwSfpWarehouse.getRemark());
        // 入库
        if (lwSfpWarehouse.getVariable() > 0) {
            lwSfpWarehouse.setQuantity(lwSfpWarehouse.getQuantity() + lwSfpWarehouse.getVariable());
            warehousingInfo.setFlow(0L);
            warehousingInfo.setCompleted(lwSfpWarehouse.getVariable());
        }
        //出库
        if (lwSfpWarehouse.getRemove() > 0){
            lwSfpWarehouse.setQuantity(lwSfpWarehouse.getQuantity() - lwSfpWarehouse.getRemove());
            warehousingInfo.setFlow(2L);
            warehousingInfo.setCompleted(lwSfpWarehouse.getRemove());
        }

        LwWarehousinginfoMapper.insertLwWarehousinginfo(warehousingInfo);

        LwSfpWarehouse sfpWarehouse = lwSfpWarehouseMapper.selectLwSfpWarehouseById(lwSfpWarehouse.getId());
        lwSfpWarehouse.setRemark(sfpWarehouse.getRemark());
        return lwSfpWarehouseMapper.updateLwSfpWarehouse(lwSfpWarehouse);
    }
}
